//
//  loginViewController.m
//  mintHome
//
//  Created by 朱彦名 on 16/6/23.
//  Copyright © 2016年 朱彦名. All rights reserved.
//

#import "loginViewController.h"
#import "RegisViewController.h"

#import "UserModal.h"
#import "NSString+MD5.h"
#import "DataBaseManager.h"

@interface loginViewController ()<UITextFieldDelegate>

@property (nonatomic, retain) UIImageView *backImg;
@property (nonatomic, retain) UITextField *usernameText;
@property (nonatomic, retain) UITextField *passwordText;
@property (nonatomic, retain) UIButton *loginButton;/**< 登录按钮*/
@property (nonatomic, retain) UIButton *registerButton;/**< 注册按钮*/
@property (nonatomic, retain) NSMutableArray *nameArr;/**< 保存数据库里所有名字的数组 */
@end

@implementation loginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"登录";

    
    [self creatSubviews];
    
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        self.view.backgroundColor = [UIColor silverColor];
    }
    else{
        self.view.backgroundColor = [UIColor whiteColor];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)creatSubviews{

    // backImg
    self.backImg = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.backImg.image = [UIImage imageNamed:@"beijing.jpeg"];
    [self.view addSubview:_backImg];
    [_backImg release];
    
    //userText
    self.usernameText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 150, self.view.frame.size.height / 15)];
    _usernameText.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 2 + 4);
    _usernameText.placeholder = @"请输入用户名";
    _usernameText.clearButtonMode = YES;
    _usernameText.clearsOnBeginEditing = YES;
    _usernameText.borderStyle = UITextBorderStyleRoundedRect;
    _usernameText.delegate = self;
    _usernameText.tag = 1000;
    [self.view addSubview:_usernameText];
    [_usernameText release];
    
    //passwordText
    self.passwordText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, _usernameText.frame.size.width, _usernameText.frame.size.height)];
    _passwordText.center = CGPointMake(_usernameText.center.x, _usernameText.center.y + _usernameText.frame.size.height + 20);
    _passwordText.placeholder = @"请输入密码";
    _passwordText.clearButtonMode = YES;
    _passwordText.clearsOnBeginEditing = YES;
    _passwordText.borderStyle = UITextBorderStyleRoundedRect;
    _passwordText.delegate = self;
    _passwordText.tag = 2000;
    _passwordText.secureTextEntry = YES;
    [self.view addSubview:_passwordText];
    [_passwordText release];
    
    //loginButton
    self.loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    //
    _loginButton.frame = CGRectMake(_usernameText.frame.origin.x, _passwordText.frame.origin.y + _passwordText.frame.size.height + 20, (_usernameText.frame.size.width - 30) / 2, self.view.frame.size.height / 19);
    _loginButton.backgroundColor = [UIColor purpleColor];
    _loginButton.layer.cornerRadius = 5;
    _loginButton.tintColor = [UIColor yellowColor];
    [_loginButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.view addSubview:_loginButton];
    
    //register
    self.registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _registerButton.frame = CGRectMake(_loginButton.frame.origin.x + _loginButton.frame.size.width + 30, _loginButton.frame.origin.y, _loginButton.frame.size.width, _loginButton.frame.size.height);
    _registerButton.backgroundColor = [UIColor purpleColor];
    _registerButton.layer.cornerRadius = 5;
    _registerButton.tintColor = [UIColor yellowColor];
    [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
    //注册按钮方法
    [_registerButton addTarget:self action:@selector(registerPage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registerButton];
    
}
#pragma mark - 键盘回收
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField endEditing:YES];
    return YES;
}
#pragma mark - 登录按钮
- (void)login:(UIButton *)button{
    
//    NSString *userMD5Str = [_usernameText.text stringFromMD5];
//    NSString *newUserStr = [userMD5Str stringByReplacingOccurrencesOfString:@"0" withString:@"1"];
//    NSString *passwordMD5Str = [_passwordText.text stringFromMD5];
//    NSString *newPassWordMD5Str = [passwordMD5Str stringByReplacingOccurrencesOfString:@"0" withString:@"1"];
    
    [[DataBaseManager shareInstance] openDB];
    
    NSMutableArray *arr = [[DataBaseManager shareInstance] select];
    
    
    for (UserModal *m in arr) {
        
        if ([m.userName isEqualToString:_usernameText.text] && [m.password isEqualToString:_passwordText.text]) {
            
            [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"buttton"];
            [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"didlogin"];
            
            [[NSUserDefaults standardUserDefaults] setObject:_usernameText.text forKey:@"userName"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
            
            [self dismissViewControllerAnimated:YES completion:^{
                
                [self.navigationController popViewControllerAnimated:YES];
                
                
                
            }];
            
        
        }else if ([_usernameText.text isEqualToString:m.userName] && ![_passwordText.text isEqualToString:m.password]){
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码错误" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
            
            
        
        }
//        //结束遍历数组
//        return;
    }
    

}



#pragma mark - 注册
- (void)registerPage{
    RegisViewController *regisPage = [[RegisViewController alloc]init];
    
    
    
    [self.navigationController pushViewController:regisPage animated:YES];
    

}



@end
